import { useAsyncList } from '@ark-ui/react/collection'

interface Post {
  userId: number
  id: number
  title: string
  body: string
}

export const InfiniteLoading = () => {
  const list = useAsyncList<Post, number>({
    autoReload: true,
    async load({ cursor }) {
      const page = cursor || 1
      const limit = 10
      const start = (page - 1) * limit

      const response = await fetch(`https://jsonplaceholder.typicode.com/posts?_start=${start}&_limit=${limit}`)

      if (!response.ok) {
        throw new Error('Failed to fetch posts')
      }

      const posts: Post[] = await response.json()
      const hasNextPage = posts.length === limit

      return {
        items: posts,
        cursor: hasNextPage ? page + 1 : undefined,
      }
    },
  })

  return (
    <div>
      <div>
        Loaded {list.items.length} posts
        {list.cursor && ` (more available)`}
      </div>

      {list.cursor && (
        <button onClick={() => list.loadMore()} disabled={list.loading}>
          {list.loading ? 'Loading...' : 'Load More'}
        </button>
      )}

      {list.error && <div>Error: {list.error.message}</div>}

      <div>
        {list.items.map((post, index) => (
          <div key={post.id}>
            <div>
              <strong>{index + 1}: </strong>
              <strong>{post.title}</strong>
            </div>
            <div>{post.body}</div>
          </div>
        ))}
      </div>
    </div>
  )
}
